#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPlainTextEdit>
#include <QMenuBar>
#include <QMenu>
#include <QFileDialog>
#include <QMessageBox>
#include <QIcon>
#include <QDesktopWidget>
#include <QTextCodec>
#include <QTextDocument>
#include <QApplication>
#include <QStatusBar>
#include <QShortcut>
#include <QFile>
#include <QFileInfo>
#include <QWindow>

#include <fstream>
#include <sstream>
#include <string.h>

#include "fontSetter.h"
#include "textFinder.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT
private slots:
    void popFontSetter();
    void popTextFinder();
    void deleteFontSetter();
    void recvFontOpts();
    void recvFontOptsAndDelete();
    bool textFindNext(QString text, bool wholeWordMode, bool caseSensitive);
    bool textFindPrev(QString text, bool wholeWordMode, bool caseSensitive);
    bool textReplace(QString text, QString replacer, bool wholeWordMode, bool caseSensitive, bool loopCallMode=false);
    void textReplaceAll(QString text, QString replacer, bool wholeWordMode, bool caseSensitive);
    void modificationCheck();
    void quickRemoveText();
    void quickRemoveTextS();
private:
    const std::string TITLE = "MyEditor";
    std::string filePath = "";
    std::ofstream writer;
    std::ifstream reader;

    int windowWidth;
    int windowHeight;

    QMenuBar* mainMenuBar;
    QMenu* fileMenu;
    QAction* newFileAction;
    QAction* openFileAction;
    QAction* saveFileAction;
    QAction* exitAction;

    QMenu* editMenu;
    QAction* autoWrapAction;
    QAction* findAction;
    QAction* readOnlyAction;
    QAction* fontAction;

    QMenu* styleMenu;
    QAction* defaultStyleAction;
    QAction* darkStyleAction;
    QAction* stickAction;

    QMenu* helpMenu;
    QAction* aboutAction;
    QPlainTextEdit *textArea;
    FontSetter* fontSetter;
    TextFinder* textFinder;

    QShortcut* quickRemoveTextSC;
    QShortcut* quickRemoveTextSCS;

    const char* DEFAULT_FONT_FAMILY = "Consolas";
    const int DEFAULT_FONT_SIZE = 11;
    const QFont::Weight DEFAULT_FONT_WEIGHT = QFont::Normal;
    const bool USE_ITALIC = false;
    const QFont DEFAULT_FONT = QFont(DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE, DEFAULT_FONT_WEIGHT, USE_ITALIC);
    QFont textFont = DEFAULT_FONT;

    std::string fontFamily = DEFAULT_FONT_FAMILY;
    int fontSize = DEFAULT_FONT_SIZE;
    QFont::Weight fontWeight = DEFAULT_FONT_WEIGHT;
    bool useItalic = USE_ITALIC;

    QString DEFAULT_STYLESHEET = qApp->styleSheet();
    std::string currentStyleName = "Default";

    std::string prevFSelPath = QDir::homePath().toStdString();
    QDir dir;

    bool readOnlyMode = false;
    bool isReadOnlyFile = false;
    void setEditMode();
    void changeReadOnlyMode();

    bool isSticky = false;
    void setStickMode();
    void changeStickMode();

    const char* appInfo = "Version: 0.28\n"
                          "This is an overly rudimentary text editor ...";
#ifdef _WIN32
    std::string appDataPath = getenv("APPDATA");
    std::string configDir = appDataPath + "\\MyEditor";
    std::string configPath = configDir + "\\settings.ini";
#else
    std::string appDataPath = getenv("HOME");
    std::string configDir = appDataPath + "/.config/myeditor";
    std::string configPath = configDir + "/settings.conf";
#endif
    std::string originalConfigStr = "";
    bool isModified = false;
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    // App Functions

    void read(const std::string filePath);
    void write(const std::string& filePath);
    void readConfigFile();
    void saveConfigFile();
    void changeItemsFont();
    void changeTextFont();

    void newFile();
    void openFile();
    void saveFile();
    void exitApp();

    void changeWrapMode();

    void aboutME();

    void setModifiedMark();
    void removeModifiedMark();
    void useDefaultStyle();
    void useDarkStyle();
    void setStyle();

    void closeEvent(QCloseEvent *event);
};
#endif // MAINWINDOW_H
